package org.freeocs.commons.velocity;

import java.util.ArrayList;
import java.util.List;

import org.freeocs.commons.util.StringUtils;

/**
 * 对TextTool的扩展,以支持整数数组
 * 
 * @author chirs
 */
public class VelocityTextTool {
	/**
	 * HTML输出内容格式转换
	 * 
	 * @param content
	 * @return
	 */
	public String html(Object content) {
		return StringUtils.formatContent((String) content);
	}

	private static List<String> tokens_keep;
	private static List<String> tokens_remove;
	static {
		tokens_keep = new ArrayList<String>();
		tokens_keep.add("&nbsp;");
		tokens_keep.add("<br/>");
		tokens_keep.add("<br />");
		tokens_remove = new ArrayList<String>();
		tokens_remove.add("<p>");
		tokens_remove.add("</p>");
	}

	/**
	 * 显示友好的格式化文本信息
	 * 
	 * @param content
	 * @return
	 */
	public static String friendly_text(String content) {
		if (content == null)
			return "";
		String html = content;
		for (int i = 0; i < tokens_remove.size(); i++) {
			html = StringUtils.remove(html, (String) tokens_remove.get(i));
		}
		List<String> randoms = new ArrayList<String>();
		for (int i = 0; i < tokens_keep.size(); i++) {
			String randomStr = String.valueOf(System.currentTimeMillis()) + '_' + i;
			html = StringUtils.replaceIgnoreCase(html, (String) tokens_keep.get(i), randomStr);
			randoms.add(randomStr);
		}
		html = StringUtils.replace(html, "&hellip;", "…");
		html = StringUtils.replace(html, "&ldquo;", "“");
		html = StringUtils.replace(html, "&rdquo;", "”");
		html = StringUtils.replace(html, "&mdash;", "—");
		html = StringUtils.replace(html, "&", "&amp;");
		html = StringUtils.replace(html, "'", "&apos;");
		html = StringUtils.replace(html, "\"", "&quot;");
		html = StringUtils.replace(html, "\t", "&nbsp;&nbsp;");// 替换跳格
		// html = StringUtils.replace(html, " ", "&nbsp;");// 替换空格
		html = StringUtils.replace(html, "<", "&lt;");
		html = StringUtils.replace(html, ">", "&gt;");
		for (int i = 0; i < tokens_keep.size(); i++) {
			String randomStr = (String) randoms.get(i);
			html = StringUtils.replaceIgnoreCase(html, randomStr, (String) tokens_keep.get(i));
		}
		return html;
	}
}
